package main;

import inteligencia_mapa.Gravedad;
import inteligencia_mapa.MoverEnAire;
import inteligencia_mapa.MovimientoMapa;
import inteligencia_personaje.ControlDeJuego;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.InputStream;
import java.util.logging.StreamHandler;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

public class Main extends javax.swing.JFrame implements KeyListener{

	
	protected ControlDeJuego controlFin;
	protected MoverEnAire movAire;
	protected Programa elPrograma;
	protected MovimientoMapa hiloMapa;
	protected boolean estaCorriendo;
	protected boolean estaSaltando;
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Main inst = new Main();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public Main() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			
			elPrograma = new Programa(getContentPane());
	
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	
			getContentPane().setPreferredSize(new Dimension(1000,448));
			getContentPane().setBackground(new java.awt.Color(100,110, 110));
			
			
			elPrograma.cargarMapa();
			hiloMapa = elPrograma.obtenerMovMapa();
			movAire = new MoverEnAire(elPrograma, hiloMapa);
			controlFin = new ControlDeJuego(hiloMapa, movAire, elPrograma);

			this.setLayout(null);
			
			hiloMapa.start();
			movAire.start();
			controlFin.start();
			
			
			estaCorriendo = false;
			estaSaltando = false;		
			
			getContentPane().setFocusable(isFocusable());
			
			getContentPane().addKeyListener(this);
	
			pack();
			
		} catch (Exception e) {
		}
	}
	
	
	public void setVisible(boolean b){
		super.setVisible(b);
		hiloMapa.refreshMapa(0);
	}
	@Override
	public void keyReleased(KeyEvent evt) {
		movAire.detener();
		hiloMapa.detener();
		estaCorriendo = false;
	}
	
	@Override
	public void keyPressed(KeyEvent evt) {
		if(evt.getKeyCode() == KeyEvent.VK_RIGHT){
			if(estaSaltando)	
				movAire.moverDerecha();
			hiloMapa.comenzarAdelante();
			estaCorriendo = true;
		}
		if(evt.getKeyCode() == KeyEvent.VK_LEFT){
			if(estaSaltando)
				movAire.moverIzquierda();
			hiloMapa.comenzarAtras();
			estaCorriendo = true;
		}
		if(evt.getKeyCode() == KeyEvent.VK_UP){
			estaSaltando = true;
			hiloMapa.saltar(estaCorriendo);
		}
		
		if(evt.getKeyCode() == KeyEvent.VK_SPACE){
			if (elPrograma.obtenerMario().obtenerdisparo())
				elPrograma.disparar();
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}
